package com.white.mybatis.dynamic.support.proxy.utils;

import com.white.mybatis.dynamic.support.proxy.LegendCglibProxy;
import com.white.mybatis.dynamic.support.proxy.TargetClassAware;

public class AopUtils {

    /**
     * 获取这个代理对象的源类
     * @param candidate 代理对象
     */
    public static Class<?> getTargetClass(Object candidate) {
        return getTarget(candidate).getClass();
    }

    /**
     * 获取这个代理对象的源类
     * @param candidate 代理对象
     */
    public static Object getTarget(Object candidate) {
        if (candidate instanceof TargetClassAware) {
            while (LegendCglibProxy.isProxyClass(candidate.getClass())) {
                candidate = ((TargetClassAware) candidate).getTarget();
            }
            return candidate;
        }
        return candidate;
    }
}
